package LinkList;

import java.util.Stack;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: LinkList
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-11-02  20:55
 * @Description: TODO
 * @Version: 1.0
 */
public class 两数相加II445 {

    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode addTwoNumbers2(ListNode l1, ListNode l2) {
        ListNode head = null, tail = null;
        int carry = 0;
        while (l1 != null || l2 != null) {
            int n1 = l1 != null ? l1.val : 0;
            int n2 = l2 != null ? l2.val : 0;
            int sum = n1 + n2 + carry;
            int curr = sum % 10;
            carry = sum / 10;
            // 一开始的head 和tail 都指向第一个元素
            if (head == null) {
                head = tail = new ListNode(curr);
            } else {
                // 然后在依次让tail.next=第二个及以后的元素 同时tail=tail.next
                tail.next = new ListNode(curr);
                tail = tail.next;
            }
            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }
        }
        if (carry > 0) {
            tail.next = new ListNode(carry);
        }
        return head;
    }

    public ListNode reverselist(ListNode head) {
        ListNode pre = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode future = curr.next;
            curr.next = pre;
            pre = curr;
            curr = future;
        }
        return pre;
    }

    // 使用的而链表的反转

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head1 = reverselist(l1);
        ListNode head2 = reverselist(l2);
        ListNode listNode = addTwoNumbers2(head1, head2);
        return reverselist(listNode);
    }

    // 使用栈来实现
    public ListNode addTwoNumbers3(ListNode l1, ListNode l2) {
        Stack<ListNode> stack1 = new Stack<>();
        Stack<ListNode> stack2 = new Stack<>();
        while (l1 != null) {
            stack1.add(l1);
            l1 = l1.next;
        }
        while (l2 != null) {
            stack2.add(l2);
            l2 = l2.next;
        }
        // 然后弹出栈
        int falg = 0;
        ListNode dumpy = new ListNode(-1);
        ListNode curr = dumpy;
        while (!stack1.isEmpty() || !stack2.isEmpty()) {
            int val1 = stack1.isEmpty() ? 0 : stack1.pop().val;
            int val2 = stack2.isEmpty() ? 0 : stack2.pop().val;
            int res = (falg + val1 + val2) % 10;
            falg = (falg + val1 + val2) / 10;
            ListNode node = new ListNode(res);
            node.next = curr.next;
            curr.next = node;
        }
        if (falg == 1) {
            ListNode node = new ListNode(falg);
            node.next = curr.next;
            curr.next = node;
        }
        return dumpy.next;
    }

}
